﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using Newtonsoft.Json;
using Tools;

namespace JHConfig
{	
	[JsonObject(MemberSerialization.OptOut)]
    public class JSConfig
    {   
        public JSConfig()
        {
        	pathconfig = new PathConfig();
        	staticsconfig = new StaticsConfig();
        	globalconfig = new GlobalConfig(pathconfig);
        	filename = "";
        }
        public void Load(string filename){
        	this.filename = filename;
        	string str = File.ReadAllText(filename);
        	JSConfig jc = JsonConvert.DeserializeObject<JSConfig>(str);
        	this.pathconfig  = jc.pathconfig;
        	this.globalconfig = jc.globalconfig;
        	this.staticsconfig = jc.staticsconfig;
            this.globalconfig.InitBaseconfig();
        }
        public void Save(string tfilename = "")
        {
        	string str = JsonFormatTool.ConvertJsonString( JsonConvert.SerializeObject(this) );
            if(tfilename == "")
                File.WriteAllText(filename, str);
            else            	
                File.WriteAllText(tfilename, str);
        }
        public PathConfig pathconfig {get;set;}      
        public StaticsConfig staticsconfig {get;set;}   
        [JsonIgnore]
        public GlobalConfig globalconfig {get;set;}
        [JsonIgnore]
        private string filename;
    }

    public class PathConfig
    {
        public PathConfig()
        {
            WorkPath = "";
            OutPath = "";
         
        }
        //public string NowWorkPath()
        //{
        //    return WorkPath + "data\\workpath\\" + TimeStringTools.NowDate() + "\\";
        //}
        //public bool CheckWorkPath()
        //{
        //    MFile.ClearEmptyPath(WorkPath + "data\\workpath\\");
        //    DirectoryInfo dir = new DirectoryInfo(NowWorkPath());
        //    dir.Create();
        //    return true;
        //} 
        public string WorkPath { get; set; }
        public string OutPath { get; set; }
        public string DebugPath { get { return OutPath + "\\Debugout\\img"; } }
       
        public  bool ExistDirectory()
        {
            if (WorkPath == "" || OutPath == "")
                return false;
            return Directory.Exists(WorkPath) && Directory.Exists(OutPath);
        }
    }
    public class StaticsConfig
    {
        public bool Debug { get; set; }
    }
    public class GlobalConfig
    {
        public GlobalConfig(PathConfig baseconfig)
        {
            this._pathconfig = baseconfig;
            _baseconfig = null;
            UpdateDebug = true;
        }
        public void InitBaseconfig()
        {
            if (_pathconfig.ExistDirectory())
            {
                _baseconfig = new ScanTemplate.BaseConfig(_pathconfig.WorkPath, _pathconfig.OutPath);
            }
        }

        public ScanTemplate.BaseConfig Baseconfig { get { return _baseconfig; } }
        public StaticsConfig Staticsconfig { get; set; }
        public bool UpdateDebug { get; set; } 
        public bool Debug { get; set; }
        public string ErrMsg { get; set; }
        //public ToolsWeb.CWeb web { get; set; }
        private PathConfig _pathconfig;
        private ScanTemplate.BaseConfig _baseconfig;

    }
}
